package com.zhiyou100.taxi.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhiyou100.taxi.dao.UserMapper;
import com.zhiyou100.taxi.entity.User;
import com.zhiyou100.taxi.entity.UserExample;
import com.zhiyou100.taxi.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashSet;
import java.util.List;

/**
 * @author zhaokuii11@163.com
 * @create 2021-11-23 19:10
 * @Description
 */
@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;


    @Override
    public User getUserByNameAndPwd(User user) {
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andUserNameEqualTo(user.getUserName());
        criteria.andUserPwdEqualTo(user.getUserPwd());
        List<User> list = userMapper.selectByExample(userExample);
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }


    @Override
    public PageInfo<User> getPage(Integer pageNum) {
        PageHelper.startPage(pageNum, 5);
        PageInfo<User> userPageInfo = new PageInfo<>(getAll());
        return userPageInfo;
    }

    @Override
    public List<User> getAll() {
        List<User> list = userMapper.selectByExample(null);
        return list;
    }

    @Override
    public int insert(User user) {
        return userMapper.insert(user);

    }

    @Override
    public String getPrimaryKey() {
        return "user_" + userMapper.selectPrimaryKey();
    }

    @Override
    public int updateByPrimaryKey(User user) {
        return userMapper.updateByPrimaryKeySelective(user);

    }

    @Override
    public User getOneByPrimaryKey(String userId) {
        return userMapper.selectByPrimaryKey(userId);
    }

    //删除包括外键
    @Override
    public int deleteByPrimaryKey(String userId) {
        return userMapper.deleteByPrimaryKey(userId);
    }

    @Override
    public int deleteByPrimaryKeys(HashSet<String> set) {
        set.forEach(u -> {
            userMapper.deleteByPrimaryKey(u);
        });
        return -1;
    }
}
